package com.operation.net.util;

import org.snmp4j.*;
import org.snmp4j.mp.MessageProcessingModel;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.PDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

import java.io.IOException;
import java.util.List;

public class GetInformation {

    //服务器接口集合
    public static void getInterface() {
        TransportMapping transport = null ;
        Snmp snmp = null ;
        CommunityTarget target;
        String[] IF_OIDS =
                {"1.3.6.1.2.1.2.2.1.1",  //Index
                        "1.3.6.1.2.1.2.2.1.2",  //descr
                        "1.3.6.1.2.1.2.2.1.3",  //type
                        "1.3.6.1.2.1.2.2.1.5",  //speed
                        "1.3.6.1.2.1.2.2.1.6",  //mac
                        "1.3.6.1.2.1.2.2.1.7",  //adminStatus
                        "1.3.6.1.2.1.2.2.1.8",  //operStatus

                        "1.3.6.1.2.1.2.2.1.10", //inOctets
                        "1.3.6.1.2.1.2.2.1.16", //outOctets
                        "1.3.6.1.2.1.2.2.1.14", //inError
                        "1.3.6.1.2.1.2.2.1.20", //outError
                        "1.3.6.1.2.1.2.2.1.13", //inDiscard
                        "1.3.6.1.2.1.2.2.1.19", //outDiscard
                        "1.3.6.1.2.1.2.2.1.11", //inUcastPkts
                        "1.3.6.1.2.1.2.2.1.17", //outUcastPkts
                        "1.3.6.1.2.1.2.2.1.12", //inNUcastPkts
                        "1.3.6.1.2.1.2.2.1.18"};//outNUcastPkts
        String[] IP_OIDS =
                {"1.3.6.1.2.1.4.20.1.1", //ipAdEntAddr
                        "1.3.6.1.2.1.4.20.1.2", //ipAdEntIfIndex
                        "1.3.6.1.2.1.4.20.1.3"};//ipAdEntNetMask
        try {
            target = InitSnmp.Init("192.168.237.15","161","public");//创建连接目标
            transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);//创建snmp
            snmp.listen();//监听消息
            TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {
                @Override
                public PDU createPDU(Target arg0) {
                    PDU request = new PDU();
                    request.setType(PDU.GET);
                    return request;
                }

                @Override
                public PDU createPDU(MessageProcessingModel messageProcessingModel) {
                    return null;
                }
            });
            OID[] columns = new OID[IF_OIDS.length];
            for (int i = 0; i < IF_OIDS.length; i++)
                columns[i] = new OID(IF_OIDS[i]);
            @SuppressWarnings("unchecked")
            List<TableEvent> list = tableUtils.getTable(target, columns, null, null);
            if(list.size()==1 && list.get(0).getColumns()==null){
                System.out.println(" null");
            }else{
                for(TableEvent event : list){
                    VariableBinding[] values = event.getColumns();
                    if(values == null) continue;
                    System.out.println("interface ---Index："+values[0].getVariable().toString()+"  descr："+values[1].getVariable().toString()+"  type："+values[2].getVariable().toString()+" speed："+values[3].getVariable().toString()+" mac："+values[4].getVariable().toString()+" adminStatus："+values[5].getVariable().toString()+"  operStatus："+values[6].getVariable().toString());
                }
            }
//获取ip
            OID[] ipcolumns = new OID[IP_OIDS.length];
            for (int i = 0; i < IP_OIDS.length; i++)
                ipcolumns[i] = new OID(IP_OIDS[i]);
            @SuppressWarnings("unchecked")
            List<TableEvent> iplist = tableUtils.getTable(target, ipcolumns, null, null);
            if(iplist.size()==1 && iplist.get(0).getColumns()==null){
                System.out.println(" null");
            }else{
                for(TableEvent event : iplist){
                    VariableBinding[] values = event.getColumns();
                    if(values == null) continue;
                    System.out.println(" IP--->ipAdEntAddr:"+values[0].getVariable().toString()+"   ipAdEntIfIndex:"+values[1].getVariable().toString()+"   ipAdEntNetMask:"+values[2].getVariable().toString());
                }
            }
        } catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if(transport!=null)
                    transport.close();
                if(snmp!=null)
                    snmp.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
